package org.agle4j.algorithm.array.unordered;

import java.util.ArrayList;
import java.util.List;

public class OperateWiteValueRepeat {
	int[] datas;
	int currentIndex = 0;

	public OperateWiteValueRepeat(int length) {
		this.datas = new int[length];
	}

	public int insert(int data) {
		datas[currentIndex] = data ;
		currentIndex ++ ;
		return this.currentIndex - 1;
	}

	public void remove(int data) {
		int index = getIndex(0, data) ;
		while(index != -1) {
			for (int i = index; i < this.currentIndex; i++) {
				datas[i] = datas[i+1] ;
			}
			currentIndex -- ;
			index = getIndex(index, data) ;
		}
	}

	public int getIndex(int begin, int data) {
		for (int i = begin; i < currentIndex; i++) {
			if (datas[i] == data) {
				return i ;
			}
		}
		return -1;
	}

	public List<Integer> searchOne(int data) {
		List<Integer> intList = new ArrayList<>();
		int index = getIndex(0, data) ;
		while (index != -1) {
			intList.add(index);
			index = getIndex(index + 1, data) ;
		}
		return intList;
	}

	public void printDatas() {
		for (int i = 0; i < this.currentIndex; i++) {
			System.out.println(this.datas[i]);
		}
		System.out.println("////////////////////");
	}
}
